<?php

namespace Beike\Seller\Http\Controllers;

use Beike\Admin\Repositories\RegionRepo;
use Beike\Seller\Models\SellerShipping;
use Beike\Seller\Repositories\SellerShippingRepo;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ShippingController extends Controller
{
    protected string $defaultRoute = 'shipping.index';

    /**
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function index()
    {
        $sellerId   = current_seller_user()->seller_id;
        $data       = [
            'regions'       => RegionRepo::getList(),
            'shipping'      => SellerShippingRepo::list($sellerId),
        ];

        return view('seller::pages.shipping.index', $data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function create(Request $request)
    {
        return $this->form($request);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {
        $requestData = $request->all();
        $shipping    = SellerShippingRepo::create($requestData);

        return redirect($this->getRedirect())->with('success', trans('common.created_success'));
    }

    /**
     * @param Request $request
     * @param int     $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function edit(Request $request, int $id)
    {
        $shipping = SellerShipping::find($id);

        return $this->form($request, $shipping);
    }

    /**
     * @param Request $request
     * @param int     $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Exception
     */
    public function update(Request $request, int $id)
    {
        $requestData = $request->all();
        $shipping    = SellerShippingRepo::update($id, $requestData);

        return redirect($this->getRedirect())->with('success', trans('common.updated_success'));
    }

    /**
     * @param Request $request
     * @param int     $id
     * @return JsonResponse
     */
    public function destroy(Request $request, int $id)
    {
        SellerShippingRepo::delete($id);

        return json_success(trans('common.deleted_success'));
    }

    /**
     * @param Request             $request
     * @param SellerShipping|null $shipping
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    protected function form(Request $request, SellerShipping $shipping = null)
    {
        $data = [
            'shipping'      => $shipping ?? new SellerShipping(),
            'regions'       => RegionRepo::getList(),
            '_redirect'     => $this->getRedirect(),
        ];

        return view('seller::pages.shipping.form', $data);
    }
}
